#include <assert.h>
#include <clock/clock.h>
#include <cpio/cpio.h>
#include <cspace/cspace.h>
#include <elf/elf.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>

#include "../elf.h"
#include "../frametable.h"
#include "../pagetable.h"
#include "../process.h"
#include "../task.h"
#include "../ut_manager/ut.h"
#include "../vmem_layout.h"

#include "../debug.h"
#define verbose SOS_DEBUG_PROC_STATUS
#include <sys/debug.h>
#include <sys/panic.h>

extern sos_pcb *_process_table;
extern int _num_procs;

int process_status(sos_pcb *pcb, int proc_num) {
    int i = 0;
    sos_pcb *info, *tmp;
    sos_pcb *result = NULL;

    if (proc_num > _num_procs) {
        sos_syscall_process_status_reply(pcb, NULL);
        return 1;
    }

    // Loop through all procs until we hit proc_num
    // Yes, this is really stupid
    HASH_ITER(hh, _process_table, info, tmp) {

        dprintf(8, "process_status: iteration %d (seeking %d)\n", i, proc_num);
        if (i == proc_num && info && info->running) {
            result = info;
            break;
        }
        i++;
    }

    dprintf(8, "Selected %p\n", info);
    sos_syscall_process_status_reply(pcb, info);
    return SOS_PROCESS_SUCCESS;
}
